package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.uis.adapters.holder.GroupMemberHolder;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.utils.GlideUtils;

import java.util.List;

/**
 * Created by Joe on 2016/12/25.
 */

public class GroupMemberAdpter extends RecyclerView.Adapter<GroupMemberHolder> {

    private Context context;
    private List<ImFriendEntivity> mFriendEntivities;
    private GroupMemberListClickListener mItemClickListener;
    private GroupMemberListLongClickListener mItemLongClickListener;
    private int type = 0 ;//由于ChatGroupMgrActivity页面改为显示名称，不知其他地方有没有公用，故加此字段，1-显示，-不显示

    public GroupMemberAdpter(Context context, List<ImFriendEntivity> friendEntivities) {
        this.context = context;
        this.mFriendEntivities = friendEntivities;
    }
    public GroupMemberAdpter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    public GroupMemberAdpter(Context context) {
        this.context = context;
    }

    public void refresh(List<ImFriendEntivity> friendEntivities) {
        this.mFriendEntivities = friendEntivities;
        notifyDataSetChanged();
    }

    public interface GroupMemberListLongClickListener {
        void onGroupMemberListLongClick(View view, int position);
    }
    public void setItemLongClickListener(GroupMemberListLongClickListener mItemLongClickListener) {
        this.mItemLongClickListener = mItemLongClickListener ;
    }

    public interface GroupMemberListClickListener {
        void onGroupMemberListClick(View view, int position);
    }

    public void setItemClickListener(GroupMemberListClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


    @Override
    public GroupMemberHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_group_member_list, parent, false);
        return new GroupMemberHolder(view, mItemClickListener,mItemLongClickListener);
    }

    @Override
    public void onBindViewHolder(GroupMemberHolder holder, int position) {

        GlideUtils.loadHeadCircularImage(context, mFriendEntivities.get(position).getHeadUrl(), holder.img);

        if (type == 1){
            holder.txt_name.setText(ToolsUtils.getNick(mFriendEntivities.get(position)));
            holder.txt_name.setVisibility(View.VISIBLE);
        }

        if ("1".equals(mFriendEntivities.get(position).getRole())){
            holder.image_identification.setBackgroundResource(R.drawable.group_create);
            //holder.img.setBackgroundResource(R.drawable.icon_female);
            holder.image_identification.setVisibility(View.VISIBLE);
            //holder.img.setVisibility(View.VISIBLE);
        }else
        if ("2".equals(mFriendEntivities.get(position).getRole())){
            holder.image_identification.setBackgroundResource(R.drawable.group_manager);
            //holder.img.setBackgroundResource(R.drawable.icon_male);
            holder.image_identification.setVisibility(View.VISIBLE);
           // holder.img.setVisibility(View.VISIBLE);
        }else {
            holder.image_identification.setVisibility(View.GONE);
            //holder.img.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        return mFriendEntivities != null ? mFriendEntivities.size() : 0;
    }
}